package deal;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import js.CsvExport;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import util.ConnectionManager;
import util.DbUtils;
public class GetDataOfHis implements Runnable{
	private static Logger logger=LogManager.getLogger(GetDataOfHis.class);
	public static int count = 0;
	//待处理数据
	public static ConcurrentLinkedQueue<Map<String,Object>> dealData = new ConcurrentLinkedQueue<Map<String,Object>>();
	@Override
	public void run() {
		logger.info("get data start!");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		//List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		String sql = "";
		sql =  "select t.phone_number,  "
				+"t.station_id, "
				+"     t.city_id, "
				+"      to_char(t.enter_time, 'yyyy/mm/dd hh24:mi:ss'), "
				+"     to_char(t.receive_time, 'yyyy/mm/dd hh24:mi:ss'), "
				+"     t.user_place, "
				+"     t.user_city, "
				+"     t.Is_Local, "
				+"     t.is_cz, "
				+"     to_char(t.operator_time, 'yyyy/mm/dd hh24:mi:ss'), "
				+" 	   s.id"
				+"  from his_base_target_users t,sms_base_station s "
				+ " where t.station_id=s.station_id and s.id is not null "
				//+ " and to_char(t.operator_time, 'yyyy/mm/dd') = to_char(sysdate-1, 'yyyy/mm/dd') "
				;
		try {
			conn = ConnectionManager.getInstance().getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
					ResultSet.CONCUR_READ_ONLY);
			stmt.setFetchSize(300);
			rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData(); //获得结果集结构信息,元数据
			int columnCount = md.getColumnCount();   //获得列数
			while (rs.next()) {
				count++;
				Map<String,Object> rowData = new LinkedHashMap<String,Object>();
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));
					/*if (i==1) {
						String noPwdMsisdn = AES.decrypt(rs.getObject(i).toString(), "HuaDongJieSuo123");
						rowData.put(md.getColumnName(i),noPwdMsisdn);
					}else {
						rowData.put(md.getColumnName(i), rs.getObject(i));
					}*/
					//System.out.println(md.getColumnName(i)+","+rs.getObject(i));
				}
				dealData.add(rowData);
				if (count %10000 ==0) {
					logger.info("get data ##"+count);
				}
			}
			CsvExport.flag=true;
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (null != rs) {
					rs.close();
					rs = null;
				}
				if (null != stmt) {
					stmt.close();
				}
			} catch (SQLException e) {

				e.printStackTrace();
			}
			DbUtils.closeQuietly(conn);
		}
	}

}
